package com.zhaoc.action;

import com.opensymphony.xwork2.ActionContext;
import com.zhaoc.domain.PageBean;
import com.zhaoc.domain.constans.Constants;
import com.zhaoc.domain.take_delivery.Promotion;
import com.zhaoc.utils.BaseAction;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.apache.commons.io.FileUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;

import javax.ws.rs.core.MediaType;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>title:com.zhaoc.action</p>
 * date:2018/8/13 0013
 * time:19:16
 * author:赵成
 * user:13566
 */
@ParentPackage("json-default")
public class PromotionAction extends BaseAction<Promotion> {

    //前台活动显示
    @Action(value = "promotion_pageQuery", results = {@Result(name = SUCCESS, type = "json")})
    public String pageQuery() {
        //使用webservice向crm系统发送请求,获取活动信息
        PageBean<Promotion> pageBean = WebClient.create(
                "http://localhost:9001/bos_management/services/promotionService/pageQuery?page=" + page + "&rows=" + rows)
                .accept(MediaType.APPLICATION_JSON)
                .get(PageBean.class);

        ActionContext.getContext().getValueStack().push(pageBean);

        return SUCCESS;
    }

    @Action(value = "promotion_showDetail")
    public String showDetail() throws IOException, TemplateException {
        //先判断对应id的html是否存在,如果存在则直接返回
        String htmlRealPath = ServletActionContext.getServletContext().getRealPath("/freemarker");
        File htmlFile = new File(htmlRealPath + "/" + model.getId() + ".html");
        //如果html文件不存在,查询数据库,中和freemarker模板生成页面
        if (!htmlFile.exists()){
            Configuration configuration = new Configuration(Configuration.VERSION_2_3_22);
            configuration.setDirectoryForTemplateLoading(new File(ServletActionContext.getServletContext()
                    .getRealPath("/WEB-INF/freemarker_templates")));

            //获取模板对象
            Template template = configuration.getTemplate("promotion_detail.ftl");

            //动态数据
            Promotion promotion = WebClient.create(Constants.BOS_MANAGEMENT_URL +
                    "/bos_management/services/promotionService/promotion" +
                    "/" + model.getId()).accept(MediaType.APPLICATION_JSON).get(Promotion.class);

            Map<String,Object> paramenterMap = new HashMap<String, Object>();
            paramenterMap.put("promotion",promotion);

            //合并输出
            template.process(paramenterMap,new OutputStreamWriter(new FileOutputStream(htmlFile),"utf-8"));
        }
        //如果存在,直接将文件返回
        ServletActionContext.getResponse().setContentType("text/html:charset=utf-8");
        FileUtils.copyFile(htmlFile,ServletActionContext.getResponse().getOutputStream());

        return NONE;
    }
}
